//
//  PersonalSettingViewModel.m
//  ChatForHot
//
//  Created by wr on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalSettingViewModel.h"
#import "ChatDemoHelper.h"

@interface PersonalSettingViewModel ()

@property (nonatomic, strong, readwrite) NSMutableArray <PersonalCellModel *> *cellDataSource;
@property (nonatomic, strong, readwrite) RACSubject *changePasswordDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *blockDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *suggesstionDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *policyDidSelectSubject;

@property (nonatomic, strong, readwrite) RACCommand *requestLogoutCommand;
@property (nonatomic, strong, readwrite) RACCommand *requestChangePassWordCommand;
@property (nonatomic, strong, readwrite) RACCommand *requestBlockCommand;
@property (nonatomic, strong, readwrite) RACCommand *requestDeleteBlockCommand;
@property (nonatomic, strong, readwrite) RACCommand *requestFeedbackCommand;

@property (nonatomic, strong, readwrite) NSMutableArray *blockDataArray;
@end

@implementation PersonalSettingViewModel

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    @weakify(self)
    
    self.blockDataArray = @[].mutableCopy;
    
    self.changePasswordDidSelectSubject = [[RACSubject alloc] init];
    self.blockDidSelectSubject = [[RACSubject alloc] init];
    self.suggesstionDidSelectSubject = [[RACSubject alloc] init];
    self.policyDidSelectSubject = [[RACSubject alloc] init];
    
    self.requestLogoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [ChatDemoHelper shareHelper].HOTCHATtabbarVC = nil;
            [ChatDemoHelper shareHelper].HOTCHATchatListView = nil;
            [ChatDemoHelper shareHelper].HOTCHATchatVC = nil;
            [UserInfoModel HOTCHATlogout];
            [[EMClient sharedClient] logout:NO completion:^(EMError *aError) {
                NSLog(@"%@",aError);
            }];
             NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
            [users setValue:input[@"accountNumber"] forKey:@"lastAccountNumber"];
            [NetWorkTool HOTCHATRequestWithStringURL:USERLOGOUT_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"GET" Parma:input SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                if (isSucess) {
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.rac_willDeallocSignal];
    }];
    
    self.requestChangePassWordCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
            [parameters setValue:[APPINFO defaultManager].accountNumber forKey:@"accountNumber"];
            [parameters setValue:[APPINFO defaultManager].accountPassword forKey:@"oldPassword"];
            [parameters setValue:input forKey:@"newPassword"];
            [NetWorkTool HOTCHATRequestWithStringURL:CHANGEPASSWORD_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"POST" Parma:parameters SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                if (isSucess) {
                    [NetWorkTool HOTCHATUpdateToken];
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.rac_willDeallocSignal];
    }];
    
    self.requestBlockCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSDictionary *dic = input;
            NSInteger page = [dic[@"currentPage"] integerValue];
            [NetWorkTool HOTCHATRequestWithStringURL:BLOCKLIST_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"GET" Parma:input SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                @strongify(self)
                if (isSucess) {
                    if (page == 1) {
                        [self.blockDataArray removeAllObjects];
                    }
                    [self.blockDataArray addObjectsFromArray:[self HOTCHATprocessDataWith:resultDictionary]];
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.rac_willDeallocSignal];
    }];
    
    self.requestDeleteBlockCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [NetWorkTool HOTCHATRequestWithStringURL:DELETEBLOCK_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"POST" Parma:input SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                if (isSucess) {
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.rac_willDeallocSignal];
    }];
    
    self.requestFeedbackCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [NetWorkTool HOTCHATRequestWithStringURL:SENDFEEDBACK_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"GET" Parma:input SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                if (isSucess) {
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.rac_willDeallocSignal];
    }];
}

- (NSArray *)HOTCHATprocessDataWith:(NSDictionary *)dic {
    NSMutableArray *result = [[NSMutableArray alloc] init];
    NSArray *array = [dic valueForKey:@"data"];
    for (NSDictionary *subdic in array) {
        UserInfoModel *model = [[UserInfoModel alloc] init];
        [model setValuesForKeysWithDictionary:subdic];
        [result addObject:model];
    }
    return result;
}

-(NSMutableArray<PersonalCellModel *> *)cellDataSource {
    @weakify(self)
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalCellModel *password = [[PersonalCellModel alloc] init];
    password.title = NSLocalizedString(@"personS1", nil);
    password.icon = @"personal_setting_password";
    password.info = @"";
    password.tag = 0;
    password.thinLineIsHidden = NO;
    password.thickLineIsHidden = YES;
    password.cellHeight = 50;
    password.handler = ^{
        @strongify(self)
        [self.changePasswordDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *block = [[PersonalCellModel alloc] init];
    block.title = NSLocalizedString(@"personS2", nil);
    block.icon = @"personal_setting_block";
    block.info = @"";
    block.thinLineIsHidden = YES;
    block.thickLineIsHidden = NO;
    block.tag = 1;
    block.cellHeight = 44 + 17.5;
    block.handler = ^{
        @strongify(self)
        [self.blockDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *suggestion = [[PersonalCellModel alloc] init];
    suggestion.title = NSLocalizedString(@"personS3", nil);
    suggestion.icon = @"personal_setting_advice";
    suggestion.tag = 2;
    suggestion.thinLineIsHidden = NO;
    suggestion.thickLineIsHidden = YES;
    suggestion.cellHeight = 44;
    suggestion.handler = ^{
        @strongify(self)
        [self.suggesstionDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *terms = [[PersonalCellModel alloc] init];
    terms.title = NSLocalizedString(@"personS4", nil);
    terms.icon = @"personal_setting_policy";
    terms.info = @"";
    terms.tag = 3;
    terms.thinLineIsHidden = NO;
    terms.thickLineIsHidden = YES;
    terms.cellHeight = 44;
    terms.handler = ^{
        @strongify(self)
        [self.policyDidSelectSubject sendNext:nil];
    };
    [cellDataSource addObjectsFromArray:@[password,block,suggestion,terms]];
    return cellDataSource;
}
@end
